package org.aeon.client.view.components.sessionPanel;

import lombok.extern.slf4j.Slf4j;
import org.aeon.client.context.GlobalContext;
import org.aeon.client.view.ApplicationUI;
import org.aeon.client.view.components.button.OperationButton;
import org.aeon.client.view.components.sessionPanel.confirmDialog.ExitGroupConfirmDialog;
import org.aeon.entity.User;
import org.aeon.entity.dto.GroupInfoDto;
import org.aeon.message.request.GroupQuitRequestMessage;
import org.aeon.utils.SimpleRequestUtil;
import org.aeon.utils.SwingUtil;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.Date;
import java.util.Objects;

/**
 * @author: MissingAeon
 * @date: 2024/2/20 14:04
 * @description:
 */
@Slf4j
public class GroupInfoPanel extends JPanel {
    private final SessionPanel sessionPanel;
    private JLabel nameLabel;
    private JLabel peopleNumberLabel;
    private JLabel creationTimeLabel;
    private JLabel describeLabel;
    private OperationButton operationBut;
    private OperationButton enterBut;

    public GroupInfoPanel(SessionPanel sessionPanel) {
        this.sessionPanel = sessionPanel;
        this.setLayout(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridheight = 15;
        constraints.insets = new Insets(60, 50, 1, 50);
        this.add(infoPanel1(), constraints);
        constraints.gridx = 0;
        constraints.gridy = 16;
        constraints.gridheight = 30;
        constraints.insets = new Insets(5, 50, 1, 50);
        this.add(infoPanel2(), constraints);
        constraints.gridx = 0;
        constraints.gridy = 46;
        constraints.gridheight = 7;
        constraints.insets = new Insets(5, 50, 60, 50);
        this.add(operationPanel(), constraints);
    }

    private JPanel operationPanel() {
        // 操作
        final JPanel panel = createPanel(new Dimension(600, 70));
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.add(Box.createHorizontalGlue());

        operationBut = new OperationButton();
        panel.add(operationBut);

        panel.add(Box.createHorizontalStrut(80));

        enterBut = new OperationButton();

        panel.add(enterBut);
        panel.add(Box.createHorizontalStrut(100));

        return panel;
    }

    private JPanel infoPanel2() {
        final JPanel panel = createPanel(new Dimension(600, 300));
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        peopleNumberLabel = new JLabel("群人数");
        panel.add(peopleNumberLabel);

        creationTimeLabel = new JLabel("创建时间");
        panel.add(creationTimeLabel);

        describeLabel = new JLabel("群描述");
        panel.add(describeLabel);
        return panel;
    }

    private JPanel infoPanel1() {
        final JPanel panel = createPanel(new Dimension(600, 150));
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        // 名称
        nameLabel = new JLabel("群名称");
        nameLabel.setFont(new Font("黑体", Font.PLAIN, 22));
        panel.add(nameLabel);
        return panel;
    }

    private JPanel createPanel(Dimension dimension) {
        final JPanel panel = new JPanel();
        // 边框
        final Border border = BorderFactory.createMatteBorder(1, 1, 1, 1,
                new Color(0xA0CECECE, true));
        panel.setBorder(border);
        SwingUtil.setSize(panel, dimension);
        return panel;
    }

    public void fillInData(GroupInfoDto group) {
        nameLabel.setText(group.getGroupName());
        peopleNumberLabel.setText(String.valueOf(group.getPeopleNumber()));
        creationTimeLabel.setText(group.getCreationTime().toString());
        describeLabel.setText(group.getGroupDescription());

        final GlobalContext context = GlobalContext.getContext();
        final User currentUser = context.getCurrentUser();
        final boolean anyMatch = group.getMembers().stream()
                .anyMatch(m -> Objects.equals(m.getId(), currentUser.getId()));
        if (anyMatch) {
            operationBut.setText("退出群聊");
            operationBut.setBackground(new Color(0xCB3737));
            operationBut.addActionListener(e -> {
                final ApplicationUI mainUI = context.getMainUI();
                final JPanel mask = SwingUtil.createMask(mainUI.getWidth(), mainUI.getHeight() - 30);
                final ExitGroupConfirmDialog dialog = new ExitGroupConfirmDialog();
                dialog.submit(event -> {
                    mainUI.removePopUpLayer(mask);
                    final GroupQuitRequestMessage reqMsg = new GroupQuitRequestMessage();
                    reqMsg.setMessage(currentUser.getId(), group.getId());
                    SimpleRequestUtil.send(context, reqMsg);
                });
                dialog.cancel(event -> mainUI.removePopUpLayer(mask));

                mask.add(dialog);
                mainUI.addPopUpLayer(mask);
            });
        } else {
            operationBut.setText("申请加入");
            operationBut.setBackground(new Color(0x4CEF67));
            operationBut.addActionListener(e -> {
                final RequestDialog dialog = new RequestDialog("申请加群");
                dialog.fillInData(group);
                dialog.setVisible(true);
            });
        }

        enterBut.setText("进入群聊");
        enterBut.setBackground(new Color(0x6DE2FF));
        enterBut.addActionListener(e -> {
            final GroupChatPanel panel = new GroupChatPanel();
            panel.fillInData(group);
            sessionPanel.add(panel);
            GlobalContext.getContext().getListColumnPanel().getMessageListPanel().addItem(group, new Date());
        });

        this.repaint();
    }
}
